package io.chenglicun.modules.clc.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.chenglicun.common.service.impl.CrudServiceImpl;
import io.chenglicun.common.constant.Constant;
import io.chenglicun.common.utils.ConvertUtils;
import io.chenglicun.modules.clc.dao.ClcVoteOptionDao;
import io.chenglicun.modules.clc.dto.ClcVoteOptionDTO;
import io.chenglicun.modules.clc.entity.ClcVoteOptionEntity;
import io.chenglicun.modules.clc.service.ClcVoteOptionService;
import io.chenglicun.modules.security.user.SecurityUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 投票选项
 *
 * @author HB 137664951@qq.com
 * @since 1.0 2023-11-24
 */
@Service
public class ClcVoteOptionServiceImpl extends CrudServiceImpl<ClcVoteOptionDao, ClcVoteOptionEntity, ClcVoteOptionDTO> implements ClcVoteOptionService {

    @Override
    public QueryWrapper<ClcVoteOptionEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<ClcVoteOptionEntity> wrapper = new QueryWrapper<>();

        Object obj = params.get("isDefault");
        wrapper.eq(ObjectUtil.isNotEmpty(obj), "is_default", obj);


        return wrapper;
    }

    @Override
    public List<ClcVoteOptionDTO> getListByVoteIds(List<Long> voteIds) {
        List<ClcVoteOptionEntity> voteOptionEntities = baseDao.selectList(new QueryWrapper<ClcVoteOptionEntity>().lambda().in(ClcVoteOptionEntity::getVoteId, voteIds));
        return ConvertUtils.sourceToTarget(voteOptionEntities, ClcVoteOptionDTO.class);
    }

    @Override
    public List<ClcVoteOptionDTO> getListByIds(List<Long> voteOptionIds) {
        List<ClcVoteOptionEntity> voteOptionEntities = baseDao.selectList(new QueryWrapper<ClcVoteOptionEntity>().lambda().in(ClcVoteOptionEntity::getId, voteOptionIds));
        return ConvertUtils.sourceToTarget(voteOptionEntities, ClcVoteOptionDTO.class);
    }

    @Override
    @Transactional
    public void saveBatch(List<ClcVoteOptionDTO> voteOptionList) {
        if (voteOptionList == null || voteOptionList.size() == 0) return;
        this.insertBatch(ConvertUtils.sourceToTarget(voteOptionList, ClcVoteOptionEntity.class));
    }

    @Override
    @Transactional
    public void deleteByVoteIds(Long... ids) {
        if (ids == null || ids.length == 0) return;
        this.deleteBatchIds(Arrays.asList(ids));

    }
}